#   Copyright 2010 Matthew Hawn

#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at

#       http://www.apache.org/licenses/LICENSE-2.0

#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

from distutils.core import setup
from distutils.extension import Extension
from warnings import *
from distutils.dep_util import newer, newer_group
from subprocess import *


if True:
    try:
        from Cython.Distutils import build_ext as _build_ext
        have_cython = True
    except ImportError:
        from distutils.command.build_ext import build_ext as _build_ext
        have_cython = False
else:
    from distutils.command.build_ext import build_ext as _build_ext
    have_cython = False



class generator(object):
    def __init__(self, command, file_map):
        self.command  = command
        self.file_map = file_map
    def check(self):
        rebuild = []
        for source, outputs in self.file_map.iteritems():
            for f in outputs:
                if newer( source, f) or newer(self.command,f):
                    rebuild.append(source)
        #print "Rebuild", rebuild
        return rebuild
    def run(self, sources):
        for f in sources:
            print "Calling %s with %s" % (self.command,f)
            check_call(['./' + self.command, f])
        
class build_ext(_build_ext):

    description = "build C/C++ and Cython extensions (compile/link to build directory)"

    sep_by = _build_ext.sep_by
    user_options = _build_ext.user_options
    help_options = _build_ext.help_options

        
    def run(self):
        #print "Generators"
        for generator in self.generators:
            sources = generator.check()
            if sources:
                generator.run(sources)
                
        #print "Base run"
        _build_ext.run(self)    
        
        #print "check dependencies"
        for source, depends in self.dependencies.iteritems():
            for depend in depends:
                if newer(depend, source):
                    print "Warning: %s newer than %s after build. Need cython to build everything" %(depend,source)
            

if have_cython:
    sources = ["pythoncl.pyx", "cl.pxi", "cl_platform.pxi"]
    build_ext.generators = [generator('generate.py', {'CL/cl.h' : ('cl.pxi',)}) ]
else:
    sources = ['pythoncl.c']
    build_ext.generators = []
    

build_ext.dependencies = {
    'pythoncl.c' : ('pythoncl.pyx', 'cl_platform.pxi', 'cl.pxi'),
    'cl.pxi' : ('generate.py', 'CL/cl.h') 
}



ext_modules = [
                Extension("pythoncl", sources,
                             include_dirs=['.'],
                             library_dirs=['/opt/ati/lib/x86/'],
                         libraries=['OpenCL']
                             ),
                ]


setup(
  name = 'Python OpenCL',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)
